package GUILayer;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MainMenuGUI {
	private JFrame frame;
	private JPanel mainPanel;
	private JPanel mainContainer;
	private JPanel mainMenuPanel;
	private JPanel customerPanel;
	private JPanel salePanel;
	private JPanel invoicePanel;
	private JPanel productPanel;
	private JPanel supplierPanel;
	private JPanel discountPanel;

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e1) {
			e1.printStackTrace();
			}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainMenuGUI window = new MainMenuGUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MainMenuGUI() {
		initialize();
	}

	private void initialize() {
		frame = new JFrame("Western Style Ltd. Sale System");
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				close();
			}
		});
		frame.setSize(1000, 700);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new CardLayout(0, 0));
		mainPanel = new JPanel();
		frame.getContentPane().add(mainPanel, "mainPanel");
		mainPanel.setLayout(new BorderLayout());
		mainMenuPanel = new JPanel();
		mainPanel.add(mainMenuPanel, BorderLayout.NORTH);
		mainMenuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

		JButton saleButton = new JButton("Sale");
		mainMenuPanel.add(saleButton);

		JButton invoiceButton = new JButton("Invoice");
		mainMenuPanel.add(invoiceButton);

		JButton customerButton = new JButton("Customer");
		mainMenuPanel.add(customerButton);

		JButton supplierButton = new JButton("Supplier");
		mainMenuPanel.add(supplierButton);

		JButton discountButton = new JButton("Discount");
		mainMenuPanel.add(discountButton);

		JButton productButton = new JButton("Product");
		mainMenuPanel.add(productButton);

		mainContainer = new JPanel();
		mainContainer.setLayout(new CardLayout());
		mainPanel.add(mainContainer, BorderLayout.CENTER);


		saleButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
				salePanel = new SaleGUI();
				mainContainer.add(salePanel, "salePanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "salePanel");
				mainContainer.updateUI();
			}
		});


		customerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
				customerPanel = new CustomerGUI();
				mainContainer.add(customerPanel, "customerPanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "customerPanel");
				mainContainer.updateUI();
			}
		});
		
		discountButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
				discountPanel = new DiscountGUI();
				mainContainer.add(discountPanel, "discountPanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "discountPanel");
				mainContainer.updateUI();
			}
		});


		invoiceButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
				invoicePanel = new InvoiceGUI();
				mainContainer.add(invoicePanel, "invoicePanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "invoicePanel");
				mainContainer.updateUI();
			}
		});


		productButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
				productPanel = new ProductGUI();
				mainContainer.add(productPanel, "productPanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "productPanel");
				mainContainer.updateUI();
			}
		});


		supplierButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				disposeAll();
			    supplierPanel = new SupplierGUI();
				mainContainer.add(supplierPanel, "supplierPanel");
				CardLayout cl = (CardLayout) (mainContainer.getLayout());
				cl.show(mainContainer, "supplierPanel");
				mainContainer.updateUI();
			}
		});
		
		saleButton.doClick();
	}
	
	
	private void disposeAll() {
		for(Component comp : mainContainer.getComponents()) {
			System.out.println("MainMenuGUI - Removing component " + comp.getClass().getName());
			mainContainer.remove(comp);
		}
	}

	public void close() {
		System.exit(1);
	}
}
